package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class LONGXIAAttr implements AdapterAttr {


    public static void main(String[] args) {
        Map<String, Object> params1 = new TreeMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("content-type", "application/x-www-form-urlencoded");
        params1.put("key", "FC72EEEF865B05F63106FC8C14D768D5");
        params1.put("merchant_no", "youyouwangyou");
        params1.put("gameid", 22);
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm("https://api.taohaoge.com/interfaces/getExpendAttribute.ashx", params1, head);
        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String yx = "http://api.longxiagame.com/s/yxname";
        Map<String,Object> params = new TreeMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("session","aLuiL70nQVGFgo1u");
        params.put("timestamp",timestamp);
        String cover = getCover(params);
        cover = cover+"timestamp="+timestamp+"&55f047d5afe6cffc191f8e0d496b3f6a";
        String sign = DigestUtils.md5Hex(cover);
        params.put("sign", sign);
        String result = HttpUtils.doPostForm(yx,params,null);
        List<JSONObject> jsonObjects = JSONObject.parseArray(result, JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("openid");
            String title = jsonObject.getString("name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        String yx = "http://api.longxiagame.com/s/yxname";
        Map<String,Object> params = new TreeMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("session","aLuiL70nQVGFgo1u");
        params.put("timestamp",timestamp);
        String cover = getCover(params);
        cover = cover+"timestamp="+timestamp+"&55f047d5afe6cffc191f8e0d496b3f6a";
        String sign = DigestUtils.md5Hex(cover);
        params.put("sign", sign);
        String response = HttpUtils.doPostForm(yx,params,null);
        List<JSONObject> data = JSONObject.parseArray(response, JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            if(gameId.equals(datum.getString("openid"))){
                String qufu = datum.getString("qufu");
                List<JSONObject> qufudata = JSONObject.parseArray(qufu, JSONObject.class);
                for(JSONObject qufudatum : qufudata) {
                    InterServerRes interServer = new InterServerRes();
                    interServer.setId(qufudatum.getString("qu"));
                    interServer.setName(qufudatum.getString("qu"));
                    interServer.setUuid(UUIDGenerator.generateUuid6());
                    String fu = qufudatum.getString("fu");
                    if(fu.contains("&")) {
                        List<InterServerRes> interServerRes1 = new ArrayList<>();
                        String[] split = fu.split("&");
                        for(String s : split){
                            InterServerRes interServer1 = new InterServerRes();
                            interServer1.setId(s);
                            interServer1.setName(s);
                            interServer1.setUuid(UUIDGenerator.generateUuid6());
                            interServerRes1.add(interServer1);
                        }
                        interServer.setInterServerRes(interServerRes1);
                    }else {
                        List<InterServerRes> interServerRes1 = new ArrayList<>();
                        InterServerRes interServer1 = new InterServerRes();
                        interServer1.setId(fu);
                        interServer1.setName(fu);
                        interServer1.setUuid(UUIDGenerator.generateUuid6());
                        interServerRes1.add(interServer1);
                        interServer.setInterServerRes(interServerRes1);
                    }
                    interServerRes.add(interServer);
                }
            }
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        InterAttrRes interAttrRes = new InterAttrRes();
        interAttrRes.setId("龙虾没有属性,不勾选!");
        interAttrRes.setName("龙虾没有属性,不勾选!");
        interAttrResList.add(interAttrRes);
        return interAttrResList;
    }
    public static String getCover(Map<String, Object> params) {
        log.info("通过Map.keySet遍历key和value：");
        StringBuffer resut = new StringBuffer();
        for (String key : params.keySet()) {
            log.info("key= " + key + " and value= " + params.get(key));
            if (key != "timestamp") {
                resut.append(key + "=" + params.get(key) + "&");
            }
        }
        return resut.toString();
    }
}